/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_UTIL_CONFIGURATION_H_
#define UCOP_UTIL_CONFIGURATION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <vector>
#include "Ucop/Macros.h"
#include "Ucop/Typedefs.h"


namespace Ucop {
namespace Util {

    class Configuration
    {
    public:
        struct SectionKV {
            std::string Key;
            std::string Value;
        };
        typedef std::vector<struct SectionKV>   SectionKVArray;

        struct Section {
            std::string Key;
            SectionKVArray Value;
            std::vector<struct Section> Children;


            const char* GetValue(const char* _Key)
            {
                for (SectionKVArray::size_type _Index = 0;
                    _Index < Value.size(); ++_Index) {
                    if (Value[_Index].Key == _Key) {
                        return Value[_Index].Value.c_str();
                    }
                }
                return NULL;
            }
        };
        typedef std::vector<struct Section> SectionArray;


        Configuration(void) {}
        virtual ~Configuration(void) {}

        virtual bool Open(const char *_Filename) = 0;

        /* For XML */
        virtual const char *GetString(const char *_Path, const char *_Default) { return _Default; }
        virtual int GetInt(const char *_Path, int _Default) { return _Default; }
        virtual long GetLong(const char *_Path, long _Default) { return _Default; }
        virtual double GetDouble(const char *_Path, double _Default) { return _Default; }
        virtual bool GetBool(const char *_Path, bool _Default) { return _Default; }

        virtual struct Section* GetSection(const char *_Path) { return NULL; }

    private:
        DISALLOW_COPY_AND_ASSIGN(Configuration);
    };

}}

#endif // UCOP_UTIL_CONFIGURATION_H_
